package com.masterclient.app.businessmodule.minemodule.collection;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.widget.empty.FCPageStateView;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.bean.personal.GoodsBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.common.config.BusinessConfig;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.minemodule.collection.adapter.GoodsCollectAdapter;

import java.util.ArrayList;
import butterknife.BindView;
import com.masterclient.app.businessmodule.minemodule.collection.contract.GoodsListContract;
import com.masterclient.app.businessmodule.minemodule.collection.contract.GoodsListPresenter;
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity;

/**
 * 作者：sean
 * 创建时间：2017/7/24 10:51
 * 功能说明：
 */
@Route(path = FCRouterPath.MINE_COLLECTION_PATH)
public class MyCollectionActivity extends FCBusinessActivity<GoodsListPresenter> implements GoodsListContract.View  {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private GoodsCollectAdapter adapter;
    int total;

    private FCPageStateView emptyView;
    private View footView;

    @Override
    protected boolean useRefreshLayout() {
        return true;
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_my_collection;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("我的收藏").setLineHidden(true);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        emptyView = new FCPageStateView(this);
        emptyView.setViewState(FCPageViewState.PAGE_STATE_NO_DATA,R.mipmap.icon_empty_favorite,"众多好物，收藏看看？");
//        footView = View.inflate(this, R.layout.bottom_common, null);

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(null);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(ScreenUtils.dp2px(4),ScreenUtils.dp2px(9),ScreenUtils.dp2px(5),0);
            }
        });

        adapter = new GoodsCollectAdapter();
        recyclerView.setAdapter(adapter);
        adapter.bindToRecyclerView(recyclerView);
        adapter.setEmptyView(emptyView);
        emptyView.setVisibility(View.GONE);
//        adapter.setFooterView(footView);
//        footView.setVisibility(View.GONE);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mPresenter.list != null && mPresenter.list.size() > 0){
                    startActivitryWithBundle(NewCommodityDetailActivity.class,new String[][]{
                            {"id",mPresenter.list.get(position).getSh_id()}, {"source_page","我的收藏"}});
                }

            }
        });

        baseRefreshLayout().setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(mPresenter.list != null){
                    mPresenter.list.clear();
                    adapter.notifyDataSetChanged();
                }
                mPresenter.getCollectGoodsList("1", BusinessConfig.PAGE_SIZE,true);
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int[] first = new int[layoutManager.getSpanCount()];
                layoutManager.findFirstCompletelyVisibleItemPositions(first);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && (first[0] == 1 || first[1] == 1)) {
                    layoutManager.invalidateSpanAssignments();
                }
            }
        });

        adapter.setOnLoadMoreListener(()->{
//            if(list.size()<total){
            mPresenter.getCollectGoodsList(String.valueOf(mPresenter.list.size()/Integer.valueOf(BusinessConfig.PAGE_SIZE)+1),BusinessConfig.PAGE_SIZE,false);
//            }
        },recyclerView);

        if(mPresenter.list != null){
            mPresenter.list.clear();
            adapter.notifyDataSetChanged();
        }
        mPresenter.getCollectGoodsList("1",BusinessConfig.PAGE_SIZE,false);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onClickPageStateErrorView() {
        super.onClickPageStateErrorView();
        mPresenter.getCollectGoodsList("1",BusinessConfig.PAGE_SIZE,false);
    }

    @Override
    public void showCollectGoodsList(GoodsBean.DataBean dataBean) {
        baseEndRefresh();
        total = Integer.parseInt(dataBean.getSh_total());
        if(mPresenter.list == null){
            mPresenter.list = new ArrayList<>();
        }
        mPresenter.list.addAll(dataBean.getSh_products());

        emptyView.setVisibility(mPresenter.list.size()<=0?View.VISIBLE:View.GONE);

        if (dataBean.getSh_products().size()>0) {
            if (mPresenter.list.size() <= 20) {
                adapter.setNewData(mPresenter.list);
            } else {
                adapter.notifyItemRangeInserted(mPresenter.list.size() - dataBean.getSh_products().size(), dataBean.getSh_products().size());
                adapter.loadMoreComplete();
            }
        }
        if (mPresenter.isHasData()) {
            if(dataBean.getSh_products() == null||dataBean.getSh_products().size()<20){
                adapter.setEnableLoadMore(false);
//                footView.setVisibility(View.VISIBLE);
            }else {
//            adapter.setEnableLoadMore(true);
//                footView.setVisibility(View.GONE);
            }
        } else {
//            footView.setVisibility(View.GONE);
        }
    }

    @Override
    public void baseShowError() {
        super.baseShowError();
        baseEndRefresh();
        if (mPresenter.list!=null && (mPresenter.list.size()/Integer.valueOf(BusinessConfig.PAGE_SIZE)+1) > 1) {
            adapter.loadMoreFail();
        }
        emptyView.setVisibility(View.GONE);
    }
}
